package ch.ethz.fcl.metrobuzz.data.texture;

import java.io.File;
import java.io.IOException;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Point2D;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Rectangle;

public class MBTexture {
	private Texture closeIcon;
	private Texture addIcon;
	private Texture northIcon;
	private Texture shadow;
	private Texture originSymbol;
	private Texture destinSymbol;

	private String folderName = "res/textures/";

	public MBTexture() {
		init();
	}

	private void init() {
		if (!new File(folderName).exists())
			folderName = "textures/";

		initIcons();
	}

	private void initIcons() {
		try {
			closeIcon = TextureLoader.getTexture(
					"PNG",
					ResourceLoader.getResourceAsStream(folderName
							+ "close32.png"));

			addIcon = TextureLoader.getTexture("PNG", ResourceLoader
					.getResourceAsStream(folderName + "add32.png"));

			northIcon = TextureLoader.getTexture("PNG", ResourceLoader
					.getResourceAsStream(folderName + "north.png"));

			shadow = TextureLoader.getTexture(
					"PNG",
					ResourceLoader.getResourceAsStream(folderName
							+ "shadow.png"));

			originSymbol = TextureLoader.getTexture(
					"PNG",
					ResourceLoader.getResourceAsStream(folderName
							+ "od_symbol_origin.png"));

			destinSymbol = TextureLoader.getTexture(
					"PNG",
					ResourceLoader.getResourceAsStream(folderName
							+ "od_symbol_destin.png"));

			GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static void initTextureRender(Texture texture) {
		GL11.glColor4f(1, 1, 1, 1);
		GL11.glEnable(GL11.GL_TEXTURE_2D);

		GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S,
				GL12.GL_CLAMP_TO_EDGE);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T,
				GL12.GL_CLAMP_TO_EDGE);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,
				GL11.GL_LINEAR);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
				GL11.GL_LINEAR);
	}

	public Texture getCloseIcon() {
		return closeIcon;
	}

	public Texture getAddIcon() {
		return addIcon;
	}

	public Texture getNorthIcon() {
		return northIcon;
	}

	public Texture getShadow() {
		return shadow;
	}

	public Texture getOriginSymbol() {
		return originSymbol;
	}

	public Texture getDestinSymbol() {
		return destinSymbol;
	}

	public static void renderTexture(Texture texture, Rectangle rec) {
		initTextureRender(texture);

		GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(0, 0);
		GL11.glVertex3f(rec.getMinX(), rec.getMaxY(), 0);

		GL11.glTexCoord2f(1, 0);
		GL11.glVertex3f(rec.getMaxX(), rec.getMaxY(), 0);

		GL11.glTexCoord2f(1, 1);
		GL11.glVertex3f(rec.getMaxX(), rec.getMinY(), 0);

		GL11.glTexCoord2f(0, 1);
		GL11.glVertex3f(rec.getMinX(), rec.getMinY(), 0);
		GL11.glEnd();

		GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
		GL11.glDisable(GL11.GL_TEXTURE_2D);
	}

	public static void renderTexture(Texture texture, Point2D p1, Point2D p2,
			Point2D p3, Point2D p4) {
		initTextureRender(texture);

		GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(0, 0);
		GL11.glVertex3f(p1.getX(), p1.getY(), 0);

		GL11.glTexCoord2f(1, 0);
		GL11.glVertex3f(p2.getX(), p2.getY(), 0);

		GL11.glTexCoord2f(1, 1);
		GL11.glVertex3f(p3.getX(), p3.getY(), 0);

		GL11.glTexCoord2f(0, 1);
		GL11.glVertex3f(p4.getX(), p4.getY(), 0);
		GL11.glEnd();

		GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
		GL11.glDisable(GL11.GL_TEXTURE_2D);
	}
}
